package com.travel.index.service.impl;

import com.travel.common.pojo.Hotel;
import com.travel.common.util.Constants;
import com.travel.index.SearchResult;
import com.travel.index.service.HotelIndexService;
import com.travel.manager.dao.HotelDAO;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class HotelIndexServiceImpl implements HotelIndexService {
    @Autowired
    private HotelDAO hotelDAO;
    @Autowired
    private SolrClient solrClient;

    @Override
    public boolean doUpdateHotelIndex(Integer hotelId) {
        try {
            if (hotelId == null) {
                throw new Exception("酒店id不能为空");
            }
            Hotel hotel = hotelDAO.getHotelById(hotelId);
            if (hotel == null) {
                throw new Exception("酒店不存在");
            }
            //如果酒店已下架就删除索引
            if (hotel.getStatus() == Constants.HOTEL_STATUS_INVALID) {
                solrClient.deleteById(hotelId.toString());
            } else {
                SolrInputDocument doc = new SolrInputDocument();
                //这个field是在solr core中配置过的
                doc.addField("id", hotel.getId());
                doc.addField("item_title", hotel.getTitle());
                doc.addField("item_content", hotel.getContent());
                doc.addField("item_city_id", hotel.getCityId());
                doc.addField("item_city_name", hotel.getCityName());
                doc.addField("item_image", hotel.getImage());
                doc.addField("item_price", hotel.getPrice());
                solrClient.add(doc);
            }
            solrClient.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public SearchResult getHotelFromIndex(String queryString, int page, int rows, boolean highLight) {
        SolrQuery query = new SolrQuery(queryString); //定义查询内容
        query.setStart((page - 1) * rows);//起始页
        query.setRows(rows);//每页显示数量
        query.setSort("id", SolrQuery.ORDER.asc);//排序
        query.setHighlight(highLight);//设置高亮
        if (highLight) {
            query.addHighlightField("item_title");//设置高亮字段
            // 设置高亮的样式
            query.setHighlightSimplePre("<font color='red'>");
            query.setHighlightSimplePost("</font>");
        }
        try {
            QueryResponse rsp = solrClient.query(query);
            SolrDocumentList results = rsp.getResults();
            List<Hotel> hotelList = new ArrayList<>();
            for (SolrDocument result : results) {
                Hotel hotel = new Hotel();
                hotel.setId(Integer.valueOf(result.get("id").toString()));
                hotel.setTitle((String) result.get("item_title"));
                hotel.setPrice(Integer.valueOf(result.get("item_price").toString()));
                hotel.setCityName((String) result.get("item_city_name"));
                hotel.setImage((String) result.get("item_image"));
                if (highLight) {
                    //取高亮显示
                    Map<String, Map<String, List<String>>> highlighting = rsp.getHighlighting();
                    List<String> list = highlighting.get(result.get("id")).get("item_title");
                    String itemTitle = "";
                    //有高亮显示的内容时。
                    if (list != null && list.size() > 0) {
                        itemTitle = list.get(0);
                    } else {
                        itemTitle = (String) result.get("item_title");
                    }
                    hotel.setTitle(itemTitle);
                }
                hotelList.add(hotel);
            }
            SearchResult result = new SearchResult();
            result.setList(hotelList);
            result.setCount(results.getNumFound());
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

}
